package com.py.framework.core.helper;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.py.framework.core.filter.SecurityCheckFilter;
import com.py.framework.core.log.ExceptionLogger;

/**
 * http辅助类
 * @author Leegern
 * @date   2018年4月27日
 */
public class HttpServletHelper {
	
	/**
	 * 获取请求参数内容
	 * @param request
	 * @return
	 */
	public static String getRequestParams(HttpServletRequest request) {
		String requestParams = "";
		if (request.getParameterMap() != null && request.getParameterMap().size() > 0) {
			Map<String, String[]> inputParamMap = request.getParameterMap();
			String[] tmpPara = null;
			for (String key : inputParamMap.keySet()) {
				tmpPara = (String[]) inputParamMap.get(key);
				// 只取非空参数
				if (tmpPara.length > 0 && StringUtils.isNotBlank(tmpPara[0])) {
					requestParams += (key + "=" + ((String[]) inputParamMap.get(key))[0] + "&");
				}
			}
			// 将参数拼接到请求地址里
			if (StringUtils.isNotBlank(requestParams)) {
				requestParams = requestParams.substring(0, requestParams.length() - 1);
			}
		}
		return requestParams;
	}
	
	/**
	 * 获取用户Token
	 * @param request
	 * @return
	 */
	public static String getUserToken(HttpServletRequest request) {
		/*
         * 首先从cookie中查找令牌, 如果不存在, 再从请求头中查找.
         */
        String jdToken    =  "";
		Cookie[] cookies  = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				if (SecurityCheckFilter.Token.equalsIgnoreCase(cookie.getName())) {
					jdToken = cookie.getValue();
					break;
				}
			}
		}
        if (StringUtils.isBlank(jdToken)) {
        	jdToken = request.getHeader(SecurityCheckFilter.Token);
        }
        if (StringUtils.isBlank(jdToken)) {
        	jdToken = request.getParameter(SecurityCheckFilter.Token);
        }
        return jdToken;
	}
	
	/**
	 * 获取来访Ip
	 * @param request
	 * @return
	 */
	public static String getGuestIP(HttpServletRequest request) {
		String ip = null;
		try {
			ip = request.getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			} else {
				String[] ips = ip.split(",");
				if (ips.length > 1) {
					for (int i = 0; i < ips.length; i++) {
						if (!ips[i].equalsIgnoreCase("unknown")) {
							ip = ips[i].trim();
							break;
						}
					}
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (ip == null) {
				ip = "";
			}
		} catch (Exception e) {
			ExceptionLogger.error(e);
		}
		return ip;
	}

	/**
	 * 获取信用评价前台网站用户Token
	 * @param request
	 * @return
	 */
	public static String getUserXypjGatewayToken(HttpServletRequest request) {
		/*
         * 首先从cookie中查找令牌, 如果不存在, 再从请求头中查找.
         */
		String xypjGatewayToken    =  "";
		Cookie[] cookies  = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				if (SecurityCheckFilter.XYPJ_GATEWAY_TOKEN.equalsIgnoreCase(cookie.getName())) {
					xypjGatewayToken = cookie.getValue();
					break;
				}
			}
		}
		if (StringUtils.isBlank(xypjGatewayToken)) {
			xypjGatewayToken = request.getHeader(SecurityCheckFilter.XYPJ_GATEWAY_TOKEN);
		}
		if (StringUtils.isBlank(xypjGatewayToken)) {
			xypjGatewayToken = request.getParameter(SecurityCheckFilter.XYPJ_GATEWAY_TOKEN);
		}
		return xypjGatewayToken;
	}
}
